package src.mcpichess.control;
import java.util.List;

import pi.Block;
import pi.Minecraft;
import pi.Vec;
import pi.event.BlockHitEvent;


/** Listens for BlockHitEvents and triggers callbacks when a block is hit **/
public class HitListener extends Thread {
	
	private Minecraft mc;
	private OnBlockHitListener onBlockHit;
	
	/** Public constructor: Give me a Minecraft instance! **/
	public HitListener(Minecraft minecraft) 
	{
		this.mc = minecraft;
	}
	
	@Override
	public void run()
	{
		System.out.println("HitListener is listening!");
		/* Loop and sleep every 5 ms */
		while(!isInterrupted())
		{
			List<BlockHitEvent> events = null;
			try{
				events = mc.events.pollBlockHits();
			}catch(Exception e){
				e.printStackTrace();
			}
			
			//if()
			
			if(this.onBlockHit != null && events != null)
			{
				if (events.size() != 0){
					BlockHitEvent ev = events.get(0);
					this.onBlockHit.onBlockHit(ev, ev.position, mc.getBlock(ev.position));
				}
			}
			
			/* Sleep */
			try { sleep(5); } 
			catch (InterruptedException e) { e.printStackTrace(); }
		}
	}
	
	/** Set our listener **/
	public void setOnBlockHitListener(OnBlockHitListener l)
	{
		this.onBlockHit = l;
	}
	
	/** Our callback interface **/
	public interface OnBlockHitListener 
	{
		public void onBlockHit(BlockHitEvent event, Vec position, Block b);
	}

}
